package com.Servlet;

import com.dao.DaoFactory;
import com.entity.book;
import com.entity.search;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class bookServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-control", "no-cache");
        System.out.println("books");
        String bookname = request.getParameter("bookname");
        String author_name = request.getParameter("author_name");

        System.out.println("bookname:" + bookname);
        System.out.println("author_name:" + author_name);

        try {
            search BK = DaoFactory.getInstance().getsearchDao().findBybookname(bookname);

            List<book> book= DaoFactory.getInstance().getbookDao().find(bookname,author_name);

            /*排序*/
            book T = new book();
            T.setAddress(null);
            T.setAuthor_name(null);
            T.setChapter_name(null);
            T.setBookname(null);
            T.setChapter_no(0);
            for(int i = 0 ; i < book.size() ; i++){
                for (int j = i+1 ; j < book.size() ;j++)
                {
                    if(book.get(i).getChapter_no() > book.get(j).getChapter_no()){
                        T.setAddress(book.get(i).getAddress());
                        T.setAuthor_name(book.get(i).getAuthor_name());
                        T.setChapter_name(book.get(i).getChapter_name());
                        T.setBookname(book.get(i).getBookname());
                        T.setChapter_no(book.get(i).getChapter_no());

                        book.get(i).setAddress(book.get(j).getAddress());
                        book.get(i).setAuthor_name(book.get(j).getAuthor_name());
                        book.get(i).setChapter_name(book.get(j).getChapter_name());
                        book.get(i).setBookname(book.get(j).getBookname());
                        book.get(i).setChapter_no(book.get(j).getChapter_no());

                        book.get(j).setAddress(T.getAddress());
                        book.get(j).setAuthor_name(T.getAuthor_name());
                        book.get(j).setChapter_name(T.getChapter_name());
                        book.get(j).setBookname(T.getBookname());
                        book.get(j).setChapter_no(T.getChapter_no());
                    }
                }
            }
            for(int i = 0 ; i < book.size() ; i++){
                System.out.println(book.get(i).getChapter_no());
            }

            /*分块*/
            List<book> list1  = new ArrayList<book>();
            List<book> list2 = new ArrayList<book>();
            List<book> list3  = new ArrayList<book>();
            book F = new book();
            book S = new book();

            int num = book.size();
            int M   = num / 3;
            for(int i = 0 , j = 0 ; i < M ; i++)
            {
                list1.add(book.get(j));
                j++;
                list2.add(book.get(j));
                j++;
                list3.add(book.get(j));
                j++;
            }
            if(num-3*M == 1){
               F = book.get(num-1);
            }
            if(num - 3*M == 2){
                F = book.get(num-2);
                S = book.get(num-1);
            }
            request.setAttribute("book",book);
            request.setAttribute("list1",list1);
            request.setAttribute("list2",list2);
            request.setAttribute("list3",list3);
            request.setAttribute("F",F);
            request.setAttribute("S",S);

            request.setAttribute("brief_introduction",BK.getBrief_introduction());




        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("book.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

}
